package com.visikard.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.visikard.ui.R;

/**
 * activity that use this fragment must implement OnImagePickedListener to receive taken image 
 * @author nvhau
 *
 */
public class KBCamera24Fragment extends BaseFragment implements ViewClickable {	
	
	private static final int REQUEST_CODE_PICK_LIBRARY = 0;
	private static final int REQUEST_CODE_PICK_INSTAGRAM = 1;
	
	private Camera24Fragment cameraFragment;
	private Uri resultFileUri;    
    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {				
		return inflater.inflate(R.layout.kb_camera_24, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {	
		super.onActivityCreated(savedInstanceState);
				       
        Bundle bundle = getArguments();
        if( bundle != null ) {        	
        	//kbuider edit kard, start this fragmetn first then start effect fragment instantly        	        	
    		resultFileUri = Uri.parse(bundle.getString(CBEffectFragment.EXT_DATA_URI));
    		fireResult();
        }
        
      //check if device have 1 or 2 camera to show swtich camera button
        View switchButton = getView().findViewById(R.id.camera_switch);
        if( Camera.getNumberOfCameras() > 1 ) {
        	switchButton.setVisibility(View.VISIBLE);
        } else {
        	switchButton.setVisibility(View.GONE);
        }
                       
        cameraFragment = (Camera24Fragment) getFragmentManager().findFragmentById(R.id.camera_fragment);
        cameraFragment.setPreviewListener(onPreviewListener);
	}
		
	/*
	 * listener for receiving camera start preview and update main UI 
	 */
	Camera24Fragment.OnPreviewListener onPreviewListener = new Camera24Fragment.OnPreviewListener() {		
		@Override
		public void onPreview() {						
			//show content after camera initial - solve sony xperia neol problem
			getActivity().getWindow().getDecorView().findViewById(android.R.id.content).setVisibility(View.VISIBLE);
		}		
	};
	
	/**
	 * get image from library
	 */
	private void getFromLibrary() {
		Intent intentPhotoLibrary = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		intentPhotoLibrary.setType("image/*");
		startActivityForResult(intentPhotoLibrary, REQUEST_CODE_PICK_LIBRARY);
	}
			
	@Override
	public void onViewClick(View v) {
		switch (v.getId()) {
		case R.id.camera_library:
			getFromLibrary();
			break;
		case R.id.camera_capture:			
			captureImage();
			break;
		case R.id.camera_switch:
			switchCamera();
			break;
		case R.id.camera_back:
			getActivity().finish();
			break;
		default:
			break;
		}		
	}			
	
	private void captureImage() {
		cameraFragment.captureImage(new Camera24Fragment.OnCaptureListener() {			
			@Override
			public void onCapture(Uri uri) {
				resultFileUri = uri;
				fireResult();
			}
		});
	}

	private void switchCamera() {
		cameraFragment.switchCamera();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if( resultCode == Activity.RESULT_OK ) {
			switch ( requestCode ) {
			case REQUEST_CODE_PICK_INSTAGRAM:				
			case REQUEST_CODE_PICK_LIBRARY:
				resultFileUri = data.getData();				
				fireResult();
				break;
			default:
				break;
			}
		}			
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	/**
	 * send result image uri to parent 
	 */
	void fireResult() {
		if( getActivity() instanceof OnImagePickedListener ) {
			((OnImagePickedListener)getActivity()).onImagePicked(resultFileUri);						
		}
	}
	
	/**
	 * interface for parent activity to implement for receiving taken image
	 * @author nvhau
	 *
	 */
	public static interface OnImagePickedListener {
		void onImagePicked(Uri uri);
	}
}
